<?php
include_once ('data_functions.inc');

function observation_page_images($observation) {
  $html = '';

  $html .= get_upload_images_button_html($observation);
  $html .= get_images_html($observation);

  return $html;
}

function get_images_html($observation) {
  $observation_id = $observation->id;
  $path = drupal_get_path('module','win_observation');
  drupal_add_js($path . '/js/external/jquery-1.3.2.js');
  drupal_add_js($path . '/js/external/ui.core.js');
  drupal_add_js($path . '/js/external/ui.draggable.js');
  drupal_add_js($path . '/js/external/ui.resizable.js');
  drupal_add_js($path . '/js/external/ui.dialog.js');
  drupal_add_js($path . '/js/win_observation_images.js');

  drupal_add_css($path . '/css/jquery-ui.css');
  drupal_add_css($path. '/css/win_observation_images.css');

  $delete_perms = FALSE;
  if (win_observation_access(array('edit any win_observation content', 'edit own win_observation content'), $observation)) {
    $delete_perms = TRUE;
  }
  $is_validated = is_observation_validated($observation_id);
  $html = '<div id="uploadedImages">';
  $imgs = get_observation_image_names($observation_id);
  $len = count($imgs);
  if ($len > 0) {
    $path = base_path() . 'sites/default/files/images/uploads/';
    $disabled = '';
    if ($is_validated || $len == 1) {
      $disabled = 'disabled';
    }
    for ($i = 0; $i < $len; $i++) {
      $img = $imgs[$i];
      $id = $img['id'];
      $src = $path . $img['tn'];
      $imgurl = $path . $img['wm'];
      $html .= <<<EOT
      <div class="galImgBox">
        <div onclick="showImage('{$imgurl}')">
          <img src="{$src}" />
        </div>
EOT;
      if ($delete_perms) {
        $html .= <<<EOT
          <div style="text-align: center">
            <input type="button" value="delete" onclick="deleteImage(this, {$observation_id}, {$id})" $disabled/>
          </div>
EOT;
      }
      $html .= <<<EOT
      </div>
EOT;
    }
  }
  else {
    $html .= 'No images have been uploaded yet.';
  }

  $html .= '</div>';

  $html .= '<div id="div_win_observation_image"><img id="win_observation_image" src="" alt="" /></div>';

  return $html;
}

function get_upload_images_button_html($observation) {
  $html = '';

  if (win_observation_access(array('edit any win_observation content', 'edit own win_observation content'), $observation)) {
    $path = drupal_get_path('module','win_observation');
    drupal_add_js($path . '/js/external/jquery-1.3.2.js');
    drupal_add_js($path . '/js/external/jquery.form.js');
    drupal_add_js($path . '/js/upload_image_handler.js');
    drupal_add_js($path . '/js/functions.js');

    $html .= '<center><input id="btnImageUpload" type="button" value="Upload Images" /></center>';
    $html .= get_upload_images_ui_html($observation->id);
  }

  return $html;
}

function get_upload_images_ui_html($observation_id) {
  $html = '';
  $base_path = base_path();

  $html .= <<<EOT
<div id="divImageUpload" title="Upload Image">
  <form action="{$base_path}observation/data" id="uploadimageform" enctype="multipart/form-data" method="post">
    <!--<input id="progress_key" name="UPLOAD_IDENTIFIER" type="hidden" value="&lt;?= $uuid ?&gt;" />-->
    <input id="imgfile[]" name="imgfile[]" type="file" /><br />
    <input id="imgfile[]" name="imgfile[]" type="file" /><br />
    <input id="imgfile[]" name="imgfile[]" type="file" /><br />
    <input id="imgfile[]" name="imgfile[]" type="file" /><br />
    <input id="imgfile[]" name="imgfile[]" type="file" />
    <br><br>
    <label for="imgCopyright">Copyright information: </label>
    <input type="textbox" id="imgCopyright" name="imgCopyright" />
    <div class="description">Please enter the copyright information for the image.</div>
    <br>
    <input type="submit" value="Upload" />
    <br><br>
    <!--<span id="uploadprogressbar" class="progressbar">0%</span>-->
    <input type="hidden" name="observation_id" id="observation_id" value="{$observation_id}"/>
    <input type="hidden" name="request" id="request" /> <!-- This is tweak to sent the parameters in JSON -->
  </form>
</div>
EOT;

  return $html;
}